package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Activity;
import cn.wolfcode.mapper.ActivityMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IActivityService;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ActivityServiceImpl implements IActivityService {

    @Autowired
    private ActivityMapper activityMapper;


    @Override
    public void save(Activity activity) {
        activityMapper.insert(activity);
    }

    @Override
    public void delete(Long id) {
        activityMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Activity activity) {
        activityMapper.updateByPrimaryKey(activity);
    }

    @Override
    public Activity get(Long id) {
        return activityMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Activity> listAll() {
        return activityMapper.selectAll();
    }

    @Override
    public PageInfo<Activity> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<Activity> activitys = activityMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Activity>(activitys);
    }

    @Override
    public List<Activity> selectByActivityId(Long id) {
        return activityMapper.selectByActivityId(id);
    }

    @Override
    public void insertRelation(Long id, Long id1, Date date) {
        //通过插入中间表东西放进去  一个员工的id . 一个你选择的旅游项目的id   ,一个事件
        if(activityMapper.checkId(UserContext.getCurrentUser().getId())==id){
            throw new RuntimeException("你已经投过票了");

        }
        //如果走过上面走到下面就证明没有投过票 那么就放进中间表中
        activityMapper.insertRealation(UserContext.getCurrentUser().getId(),id1,new Date());
    }

    @Override
    public void updateNum(Long id) {
        activityMapper.updateNum(id);
    }

    @Override
    public List<Activity> selectByInId(Long id) {
        return activityMapper.selectByInId(id);
    }

    @Override
    public Integer selectNum(Long id) {
        return activityMapper.selectNum(id);
    }

    @Override
    public Integer selectByNumtoId(long id) {
        return activityMapper.selectByNumId(id);
    }
}
